from gis.common import track_logging

from django.utils.decorators import method_decorator

from gis.common.django_ext.decorators import validate_parameters
from gis.common.django_ext.views import BaseView, PaginationResponse
from gis.common.exceptions import BizException
from itpay.core.agency import transation_service
from itpay.server_agency.decorators import require_login
from .agency_recharge_forms import *
from itpay.core.agency import agency_service
from itpay.core.agency.errors import *
from itpay.core.agency.const import AgencyType
from .helper import wrapper_agency_name

_LOGGER = track_logging.getLogger(__name__)


class ChildAgencyRechargeOrderView(BaseView):
    @method_decorator(require_login())
    @method_decorator(validate_parameters(ChildAgencyRechargeOrderListSchema))
    def get(self, request, cleaned_data):
        user_id = request.user_id
        total, order = transation_service.list_apply_recharge(parent_id=user_id, **cleaned_data)
        return PaginationResponse(total, [wrapper_agency_name(e) for e in order])


class MyRechargeOrderView(BaseView):
    @method_decorator(require_login())
    @method_decorator(validate_parameters(MyRechargeOrderListSchema))
    def get(self, request, cleaned_data):
        user_id = request.user_id
        total, order = transation_service.list_apply_recharge(agency_id=user_id, **cleaned_data)
        return PaginationResponse(total, order)

    @method_decorator(require_login(check_vcode=True))
    @method_decorator(validate_parameters(MyRechargeOrderAddSchema))
    def post(self, request, cleaned_data):
        user_id = request.user_id
        order = transation_service.apply_recharge(user_id, cleaned_data['amount'])
        return order


class RechargeReportListView(BaseView):
    @method_decorator(require_login())
    @method_decorator(validate_parameters(RechargeReportListSchema))
    def get(self, request, cleaned_data):
        user = request.user
        if AgencyType(user['agency_type']) == AgencyType.ADMIN_AGENCY:
            cleaned_data['parent_id'] = user['id']
        elif AgencyType(user['agency_type']) == AgencyType.NORMAL_AGENCY:
            cleaned_data['agency_id'] = user['id']
        total, resp_items = transation_service.list_recharge_report(**cleaned_data)
        return PaginationResponse(total, resp_items)


class AgencyDailyReportView(BaseView):
    @method_decorator(require_login())
    @method_decorator(validate_parameters(TtlRechargeReportSchema))
    def get(self, request, cleaned_data):
        user = request.user
        if AgencyType(user['agency_type']) == AgencyType.ADMIN_AGENCY:
            cleaned_data['parent_id'] = user['id']
        elif AgencyType(user['agency_type']) == AgencyType.NORMAL_AGENCY:
            cleaned_data['agency_id'] = user['id']
        resp_items = transation_service.get_daily_report(**cleaned_data)
        return resp_items


class AgencyRechargeAgreeView(BaseView):
    @method_decorator(require_login(check_vcode=True))
    @method_decorator(validate_parameters(AgencyOrderChangeStatusSchema))
    def put(self, request, order_id, cleaned_data):
        _check_permission(order_id, request.user_id)
        transation_service.agree_recharge(order_id, **cleaned_data)
        return dict(id=order_id)


class AgencyRechargeRefuseView(BaseView):
    @method_decorator(require_login(check_vcode=True))
    @method_decorator(validate_parameters(AgencyOrderChangeStatusSchema))
    def put(self, request, order_id, cleaned_data):
        _check_permission(order_id, request.user_id)
        transation_service.refuse_agency_recharge(order_id, **cleaned_data)
        return dict(id=order_id)


class OrderChaseView(BaseView):
    @method_decorator(require_login(check_vcode=True))
    @method_decorator(validate_parameters(AgencyOrderChangeStatusSchema))
    def put(self, request, order_id, cleaned_data):
        _check_permission(order_id, request.user_id)
        transation_service.chase_recharge(order_id, **cleaned_data)
        return dict(id=order_id)


def _check_permission(order_id, user_id):
    order = transation_service.get_recharge_apply_by_id(order_id)
    if not agency_service.check_agency_parent(order['agency_id'], user_id):
        raise BizException(ERROR_AGREE_AGENCY_NOT_BELONG)
